from .graph_database import GRAPH_DB_CONN

class AnswerSearcher:
    def __init__(self):
        self.g = GRAPH_DB_CONN
        self.num_limit = 20

    '''执行cypher查询，并返回相应结果'''
    def search_main(self, sqls):
        final_answers = []
        for sql_ in sqls:
            question_type = sql_['question_type']
            queries = sql_['sql']
            answers = []
            for query in queries:
                ress = self.g.run(query).data()
                answers += ress
            print(answers)
            final_answer = self.answer_prettify(question_type, answers)
            if final_answer:
                final_answers.append(final_answer)
        return final_answers

    '''根据对应的qustion_type，调用相应的回复模板'''
    def answer_prettify(self, question_type, answers):
        final_answer = []
        if not answers:
            return ''

        elif question_type == 'stockid_conceptget':
            desc = [i['n.名称'] for i in answers]
            subject1 = answers[0]['m.股票代码']
            subject2 = answers[0]['m.股票名称']
            final_answer = '{0} {1}的所属概念是：{2}'.format(subject1, subject2, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'stockname_conceptget':
            desc = [i['n.名称'] for i in answers]
            subject1 = answers[0]['m.股票代码']
            subject2 = answers[0]['m.股票名称']
            final_answer = '{0} {1}的所属概念是：{2}'.format(subject1, subject2, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'concept_stockget':
            desc = [i['m.股票代码'] + ' ' + i['m.股票名称'] for i in answers]
            length = len(answers[0])
            list_subject = []
            if length > 2:
                for i in range(2, length):
                    list_subject.append(answers[0]['n#i#.名称'.replace('#i#', str(i-2))])
            subject = "、".join(list_subject)
            final_answer = '属于{0}概念的股票有：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'company_chairmanget':
            # print(answers)
            # desc = [i['m.名称'] for i in answers]
            subject1 = answers[0]['n.股票代码']
            subject2 = answers[0]['n.名称']
            subject3 = answers[0]['m.名称']
            subject4 = answers[0]['m.年龄']
            subject5 = answers[0]['m.性别']
            subject6 = answers[0]['m.学历']
            subject7 = answers[0]['m.薪酬']
            final_answer = '{0} {1}的董事长是：{2},{3} {4} {5} 薪酬：{6}'.format(subject1, subject2,subject3,subject4,subject5,subject6,subject7)

        elif question_type == 'stockid_chairmanget':
            # print(answers)
            subject1 = answers[0]['n.股票代码']
            subject2 = answers[0]['n.名称']
            subject3 = answers[0]['m.名称']
            subject4 = answers[0]['m.年龄']
            subject5 = answers[0]['m.性别']
            subject6 = answers[0]['m.学历']
            subject7 = answers[0]['m.薪酬']
            final_answer = '{0} {1}的董事长是：{2},{3} {4} {5} 薪酬：{6}'.format(subject1, subject2, subject3, subject4,
                                                                               subject5, subject6, subject7)

        elif question_type == 'stockname_chairmanget':
            subject1 = answers[0]['n.股票代码']
            subject2 = answers[0]['n.名称']
            subject3 = answers[0]['m.名称']
            subject4 = answers[0]['m.年龄']
            subject5 = answers[0]['m.性别']
            subject6 = answers[0]['m.学历']
            subject7 = answers[0]['m.薪酬']
            final_answer = '{0} {1}的董事长是：{2},{3} {4} {5} 薪酬：{6}'.format(subject1, subject2, subject3, subject4,
                                                                               subject5, subject6, subject7)

        elif question_type == 'company_industryget':
            # print(answers)
            desc = [i['n.名称'] for i in answers]
            subject1 = answers[0]['m.股票代码']
            subject2 = answers[0]['m.名称']
            final_answer = '{0} {1}的所属行业是：{2}'.format(subject1, subject2,
                                                            '-'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'stockid_industryget':
            # print(answers)
            desc = [i['n.名称'] for i in answers]
            subject1 = answers[0]['m.股票代码']
            subject2 = answers[0]['m.股票名称']
            final_answer = '{0} {1}的所属行业是：{2}'.format(subject1, subject2, '-'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'stockname_industryget':
            desc = [i['n.名称'] for i in answers]
            subject1 = answers[0]['m.股票代码']
            subject2 = answers[0]['m.股票名称']
            final_answer = '{0} {1}的所属行业是：{2}'.format(subject1, subject2, '-'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'industry_stockget':
            desc = [i['m.股票代码'] + ' ' + i['m.股票名称'] for i in answers]
            subject = answers[0]['n.名称']
            final_answer = '属于{0}行业的股票有：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'stockid_indextypeget':
            desc = [i['n.名称'] for i in answers]
            subject1 = answers[0]['m.代码']
            subject2 = answers[0]['m.名称']
            final_answer = '将{0} {1}纳入的指数类有：{2}'.format(subject1, subject2, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'stockname_indextypeget':
            desc = [i['n.名称'] for i in answers]
            subject1 = answers[0]['m.代码']
            subject2 = answers[0]['m.名称']
            final_answer = '将{0} {1}纳入的指数类有：{2}'.format(subject1, subject2, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'indextype_stockget':
            desc = [i['m.代码'] + ' ' + i['m.名称'] for i in answers]
            length = len(answers[0])
            list_subject = []
            if length > 2:
                for i in range(2, length):
                    list_subject.append(answers[0]['n#i#.名称'.replace('#i#', str(i-2))])
            subject = "、".join(list_subject)
            final_answer = '纳入{0}的股票有：{1}'.format(subject, '；'.join(list(set(desc))[:self.num_limit]))

        elif question_type == 'company_cominfoget':
            desc = [i['m.名称'] for i in answers]
            subject1 = answers[0]['m.名称']
            subject2 = answers[0]['m.股票代码']
            subject3 = answers[0]['m.股票名称']
            subject4 = answers[0]['m.主营业务']
            subject5 = answers[0]['m.办公地址']
            subject6 = answers[0]['m.注册资金']
            subject7 = answers[0]['m.公司简介']
            final_answer = '{0} 发行股票有：{1} {2},主营业务：{3} 办公地址：{4} 注册资金：{5} 公司简介：{6}'.format(subject1, subject2,subject3, subject4,subject5, subject6,subject7,'；'.join(list(set(desc))[:self.num_limit])).replace('\n', '')
            final_answer += '\n'

        elif question_type == 'stockid_cominfoget':
            desc = [i['m.名称'] for i in answers]
            subject1 = answers[0]['m.名称']
            subject2 = answers[0]['m.股票代码']
            subject3 = answers[0]['m.股票名称']
            subject4 = answers[0]['m.主营业务']
            subject5 = answers[0]['m.办公地址']
            subject6 = answers[0]['m.注册资金']
            subject7 = answers[0]['m.公司简介']
            final_answer = '{0} 发行股票有：{1} {2},主营业务：{3} 办公地址：{4} 注册资金：{5} 公司简介：{6}'.format(subject1, subject2,subject3, subject4,subject5, subject6,subject7,'；'.join(list(set(desc))[:self.num_limit])).replace('\n', '')

        elif question_type == 'stockname_cominfoget':
            desc = [i['m.名称'] for i in answers]
            subject1 = answers[0]['m.名称']
            subject2 = answers[0]['m.股票代码']
            subject3 = answers[0]['m.股票名称']
            subject4 = answers[0]['m.主营业务']
            subject5 = answers[0]['m.办公地址']
            subject6 = answers[0]['m.注册资金']
            subject7 = answers[0]['m.公司简介']
            final_answer = '{0} 发行股票有：{1} {2},主营业务：{3} 办公地址：{4} 注册资金：{5} 公司简介：{6}'.format(subject1, subject2,subject3, subject4,subject5, subject6,subject7,'；'.join(list(set(desc))[:self.num_limit])).replace('\n', '')

        elif question_type == 'company_stockholderget':
            desc = [i['m.名称'] for i in answers]
            subject1 = answers[0]['n.股票代码']
            subject2 = answers[0]['n.名称']
            final_answer = '{0} {1}的主要股东有：{2}'.format(subject1, subject2,'；'.join(list(set(desc))[:self.num_limit])).replace('\n', '')

        elif question_type == 'stockid_stockholderget':
            desc = [i['m.名称'] for i in answers]
            subject1 = answers[0]['n.股票代码']
            subject2 = answers[0]['n.名称']
            final_answer = '{0} {1}的主要股东有：{2}'.format(subject1, subject2,'；'.join(list(set(desc))[:self.num_limit])).replace('\n', '')

        elif question_type == 'stockname_stockholderget':
            desc = [i['m.名称'] for i in answers]
            subject1 = answers[0]['n.股票代码']
            subject2 = answers[0]['n.名称']
            final_answer = '{0} {1}的主要股东有：{2}'.format(subject1, subject2,'；'.join(list(set(desc))[:self.num_limit])).replace('\n', '')

        elif question_type == 'company_subcompanyget':
            desc = [i['n.名称'] for i in answers]
            subject1 = answers[0]['m.股票代码']
            subject2 = answers[0]['m.名称']
            final_answer = '{0} {1}参股或控股的子公司有：{2}'.format(subject1, subject2,'；'.join(list(set(desc))[:self.num_limit])).replace( '\n', '')

        elif question_type == 'stockid_subcompanyget':
            desc = [i['n.名称'] for i in answers]
            subject1 = answers[0]['m.股票代码']
            subject2 = answers[0]['m.名称']
            final_answer = '{0} {1}参股或控股的子公司有：{2}'.format(subject1, subject2,'；'.join(list(set(desc))[:self.num_limit])).replace('\n', '')

        elif question_type == 'stockname_subcompanyget':
            desc = [i['n.名称'] for i in answers]
            subject1 = answers[0]['m.股票代码']
            subject2 = answers[0]['m.名称']
            final_answer = '{0} {1}参股或控股的子公司有：{2}'.format(subject1, subject2,'；'.join(list(set(desc))[:self.num_limit])).replace('\n', '')

        return final_answer


if __name__ == '__main__':
    searcher = AnswerSearcher()